#! /bin/sh

# chkconfig: 2345 20 80
# description: Start ans stop InfiniBand IPoIB partitioning daemon

### BEGIN INIT INFO
# Provides:          ib-enhanced-serices
# Required-Start:    
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start:     3 5
# Default-Stop:
# Short-Description: Starts and stops gvd
#
### END INIT INFO

is_gvd(){
	pid=$(pidof gvd|cut -f1 -d" ")
	if [ -z "$pid" ] ; then
		echo 0
	else
		echo $pid
	fi
}
stop_gvd(){
	stat=$(is_gvd)
	if [ $stat -eq 0 ] ; then
		return
	fi
	/usr/voltaire/gvdctl exit
	stat=$(is_gvd)
	if [ $stat -ne 0 ] ; then
		killall -9 gvd &> /dev/null
	fi
	
}
start_gvd(){
	stat=$(is_gvd)
	if [ $stat -eq 0 ] ; then
		stop_gvd
	fi
	if [ $start_gvd == "y" ] ; then
		/usr/voltaire/gvd
	fi
}

if [ -r /usr/voltaire/gs.conf ]; then
        . /usr/voltaire/gs.conf
fi
if [ -z $start_gvd ] ; then
	start_gvd=y
fi
case "$1" in
        start)
		start_gvd
                ;;
        stop)
		stop_gvd
                ;;
        status)
		stat=$(is_gvd)
		if [ $stat -ne 0 ] ; then
			echo gvd
		fi
                ;;
        restart)
		stop_gvd
		start_gvd
                ;;
        *)
                echo "Usage: $0 {start|stop|restart|status}"
                exit 1
                ;;
esac

exit 0

